﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Threading;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.WebControls;
using System.Drawing;

namespace Macaw.Moss2007.UploadSolution
{
    public class UploadSolutionPackagePage : GlobalAdminPageBase
    {
        //Fields
        //private ButtonSection _doneButtons;
        //private InputFormSection UploadSection;
        //private HtmlInputFile _textFileName;
        protected System.Web.UI.WebControls.Label lblErrorMessage;
        protected HtmlInputFile TextFileName;

        private string strTempPath = Path.GetTempPath();

        public UploadSolutionPackagePage()
        {
        }

        protected void BtnOk_Clicked(object sender, EventArgs e)
        {
            string strPackageFileName = Path.GetFileName(TextFileName.PostedFile.FileName);
            string strPackageServerFileName = strTempPath + strPackageFileName;
            lblErrorMessage.ForeColor = Color.Red;

            try
            {
                //Checking if the file selected...
                if (TextFileName.Value == "")
                {
                    lblErrorMessage.Text = "Please select the solution file";
                    return;
                }

                //Checking if the package already existed in the Farm Solutions.

                using (SPLongOperation operation = new SPLongOperation(this.Page))
                {
                    operation.LeadingHTML = "Uploading the solution package";
                    operation.TrailingHTML = "Adding " + strPackageFileName + " to the solution store..";
                    operation.Begin();

                    //Deleting the file if its already existed.
                    if (File.Exists(strPackageServerFileName))
                        File.Delete(strPackageServerFileName);

                    //Saving a copy of the Package.
                    TextFileName.PostedFile.SaveAs(strPackageServerFileName);


                    if (SPFarm.Local.Solutions[strPackageFileName] != null)
                    {
                        SPSolution solution = SPFarm.Local.Solutions[strPackageFileName];
                        solution.Upgrade(strPackageServerFileName);
                    }
                    else
                    {
                        //Adding the Package to the Store.
                        SPFarm.Local.Solutions.Add(strPackageServerFileName);
                    }
                    Thread.Sleep(0x1770);
                    operation.End(SPContext.Current.Site.Url + "/_admin/operations.aspx");

                }

            }
            catch (Exception ex)
            {

                throw new SPException(ex.Message);
            }


        }



    }
}
